package cn.org.timespace.ons.service.base.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.ons.action.base.MachinesModel;
import cn.org.timespace.ons.action.base.TypeAuditModel;
import cn.org.timespace.ons.dao.base.MachineTypeNameValueDao;
import cn.org.timespace.ons.dao.base.MachinesApplyDao;
import cn.org.timespace.ons.dao.base.MachinesConfDao;
import cn.org.timespace.ons.dao.base.MachinesDao;
import cn.org.timespace.ons.dao.base.TypeAuditDao;
import cn.org.timespace.ons.dao.base.UserMachineTypeDao;
import cn.org.timespace.ons.service.base.MachinesService;
import cn.org.timespace.ons.util.ResultUtil;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.MachineTypeNameValue;
import cn.org.timespace.ons.vo.base.Machines;
import cn.org.timespace.ons.vo.base.MachinesApply;
import cn.org.timespace.ons.vo.base.MachinesConf;
import cn.org.timespace.ons.vo.base.Options;
import cn.org.timespace.ons.vo.base.TypeAudit;
import cn.org.timespace.ons.vo.base.UserMachineType;

public class MachinesServiceImpl implements MachinesService {
	
	private MachinesDao tsMachinesDao;
	private UserMachineTypeDao userMachineTypeDao;
    private MachineTypeNameValueDao machineTypeNameValueDao;
    private MachinesConfDao machinesConfDao;
    private TypeAuditDao typeAuditDao;
    private MachinesApplyDao machinesApplyDao;
	
    public MachinesApplyDao getMachinesApplyDao() {
		return machinesApplyDao;
	}
	public void setMachinesApplyDao(MachinesApplyDao machinesApplyDao) {
		this.machinesApplyDao = machinesApplyDao;
	}
	public TypeAuditDao getTypeAuditDao() {
		return typeAuditDao;
	}
	public void setTypeAuditDao(TypeAuditDao typeAuditDao) {
		this.typeAuditDao = typeAuditDao;
	}
	
	public MachinesConfDao getMachinesConfDao() {
		return machinesConfDao;
	}

	public void setMachinesConfDao(MachinesConfDao machinesConfDao) {
		this.machinesConfDao = machinesConfDao;
	}
	
	
	public MachineTypeNameValueDao getMachineTypeNameValueDao() {
		return machineTypeNameValueDao;
	}

	public void setMachineTypeNameValueDao(
			MachineTypeNameValueDao machineTypeNameValueDao) {
		this.machineTypeNameValueDao = machineTypeNameValueDao;
	}

	@Override
	public List<Machines> findByCondition(MachinesModel model) {
		// TODO Auto-generated method stub
		return tsMachinesDao.findByCondition(model);
	}

	
	public UserMachineTypeDao getUserMachineTypeDao() {
		return userMachineTypeDao;
	}



	public void setUserMachineTypeDao(
			UserMachineTypeDao userMachineTypeDao) {
		this.userMachineTypeDao = userMachineTypeDao;
	}



	public MachinesDao getTsMachinesDao() {
		return tsMachinesDao;
	}

	public void setTsMachinesDao(MachinesDao tsMachinesDao) {
		this.tsMachinesDao = tsMachinesDao;
	}

	@Override
	public List<Object> findByCondition(MachinesModel model, Pager p) {
		// TODO Auto-generated method stub
		return tsMachinesDao.findByCondition(model, p);
	}

	@Override
	public void delete(Machines persistentInstance) {
		// TODO Auto-generated method stub
		tsMachinesDao.delete(persistentInstance);
		
	}

	@Override
	public Machines findById(String id) {
		// TODO Auto-generated method stub
		return tsMachinesDao.findById(id);
	}

	@Override
	public void save(Machines transientInstance) {
		// TODO Auto-generated method stub
		tsMachinesDao.save(transientInstance);
		
	}

	@Override
	public void update(Machines instance) {
		// TODO Auto-generated method stub
		tsMachinesDao.update(instance);
	}
	@Override
	public List<Object> findByCondition(Pager p) {
		// TODO Auto-generated method stub
		return tsMachinesDao.findByCondition(p);
	}

	
	
	public List<Options> findByCode(String code) {
		// TODO Auto-generated method stub
		return this.tsMachinesDao.findByCode(code);
	}

	@Override
	public List<UserMachineType> doListMachineTypeName(String userName) {
		// TODO Auto-generated method stub
		return this.tsMachinesDao.doListMachineTypeName(userName);
	}

	@Override
	public Machines findByCondition(String compCode, 
			String deviceCode, String deviceRandom) {
		// TODO Auto-generated method stub
		return this.tsMachinesDao.findByCondition(compCode, deviceCode, deviceRandom);
	}

	@Override
	public Machines findByCondition(String deviceCodes){
		return this.tsMachinesDao.findByCondition(deviceCodes);
	}
	
	@Override
	public ResultUtil getListMachines(String compCode,String userName,List<String[]> list){
		ResultUtil resultUtil = new ResultUtil();
		List<Machines> listMachines = new ArrayList<Machines>();
		Map<String,String> typeMap = new HashMap<String,String>();
		List<String> codeList = new ArrayList<String>();
		for(String item[]:list){
			String deviceCode = item[0];//设备编号
			String name = item[1];//设备类型
			String theIdmachines = typeMap.get(name);
			if(theIdmachines == null){
				UserMachineType userMachineType = userMachineTypeDao.doCheckOptionsNameIsExist(userName,name);
				if(userMachineType == null){
					resultUtil.setStatus("1");
					resultUtil.setResult("类型 "+name+" 不存在,请检查!");
					return resultUtil;
				}else{
					theIdmachines = userMachineType.getTheIdMachine();
					typeMap.put(name, theIdmachines);
				}
			}
			
			boolean codeFlag = codeList.contains(deviceCode);//查询是否已经存在此编号
			if(codeFlag == true){
				resultUtil.setStatus("2");
				resultUtil.setResult("信息终端编号 "+deviceCode+" 存在,请检查!");
				return resultUtil;
			}else{
				Machines m = this.findByCondition(compCode, deviceCode, "");
				if(m == null){
					codeList.add(deviceCode);
				}else{
					resultUtil.setStatus("2");
					resultUtil.setResult("信息终端编号 "+deviceCode+" 存在,请检查!");
					return resultUtil;
				}
				
			}
			
			Machines machines = new Machines();
			machines.setMid(IdentityGenerator.getInstance().getmacId());//主键
			machines.setCompCode(compCode);//厂家编码
			machines.setDeviceCode(deviceCode);//设备编码
			String deviceRandom = IdentityGenerator.getInstance().getCharacterAndNumber(6);//设备随机码
			machines.setDeviceRandom(deviceRandom);
			machines.setType("0000000000000000");//设备状态
			machines.setName(deviceCode);//名字
			machines.setRemark(deviceCode);//备注
			machines.setStatus(1);//设备上线
			machines.setAddress("");//地址
			machines.setUserName(userName);//用户
			
			machines.setTheIdMachine(theIdmachines);
			String machinesID = compCode+deviceCode+deviceRandom;
			machines.setMachinesID(machinesID);//三码组合
			machines.setCreateTime(new Date());//导入时间
			
			listMachines.add(machines);
			
		}
		resultUtil.setStatus("0");
		resultUtil.setResult("导入成功");
		resultUtil.setMachinesList(listMachines);
		return resultUtil;
	}
	
	@Override
	public void insertBatch(List<Machines> list){
		for(Machines machines:list){
			String mid = machines.getMid();//信息终端ID
			String theIdmachine = machines.getTheIdMachine();//信息终端类型
			String startUserName = machines.getUserName();
			List<MachineTypeNameValue> listNameValue = machineTypeNameValueDao.findByCondition(machines.getTheIdMachine());
			for(MachineTypeNameValue mnv:listNameValue){
				String confName = mnv.getName();//信息终端类型值名称
				String confValue = mnv.getValue();//信息终端类型值
				MachinesConf machinesConf = new MachinesConf();
				machinesConf.setMid(mid);
				machinesConf.setConfName(confName);
				machinesConf.setConfValue(confValue);
				machinesConfDao.save(machinesConf);//保存信息终端的配置值
			}
			
			TypeAuditModel typeAuditModel = new TypeAuditModel();
			typeAuditModel.setTheIdmachine(theIdmachine);
			typeAuditModel.setStatus(0);
			typeAuditModel.setStartUserName(startUserName);
			List<TypeAudit> listTypeAudit = typeAuditDao.findByCondition(typeAuditModel);
			for(TypeAudit typeAudit:listTypeAudit){
				String asid = typeAudit.getTheIdapply();
				String endUserName = typeAudit.getEndUserName();
				MachinesApply machinesApply = new MachinesApply();
				machinesApply.setMsid(mid);
				machinesApply.setAsid(asid);
				machinesApply.setStatus(0);
				machinesApply.setSentTime(new Date());
				machinesApply.setStartUserName(startUserName);
				machinesApply.setEndUserName(endUserName);
				machinesApplyDao.save(machinesApply);
			}
			
			this.save(machines);
		}
		//this.tsMachinesDao.insertBatch(list);
	}
	@Override
	public List<Object> findByBatch() {
		// TODO Auto-generated method stub
		return tsMachinesDao.findByBatch();
	}
}
